import { useState, useEffect } from "react";
import { useApp } from '@inbiz/utils';
import { getMessageAllGroup, getMessageGroup } from '../service';

export const useGetGroups = (props: { h5Viewtype: string; msgGroupLayout: string; range: string; msgGroup: any[]; iconGrid: any[]; listGroup: any[]; }) => {
    const { h5Viewtype, msgGroupLayout, range, msgGroup, iconGrid, listGroup } = props;
    const appInfo = useApp();
    const [loading, setLoading] = useState<boolean>(false);
    const [data, setData] = useState<any>([]);

    const getGroups = async () => {
        const _range = (h5Viewtype == 'list' && msgGroupLayout == 'listAndGrid') ? 'listAndGrid' : range;
        let result: any = [];
        setLoading(true);
        switch (_range) {
            case 'current':
                // 当前应用
                let _groups = await getMessageAllGroup({
                    appId: appInfo?.AppId,
                    pageIndex: 1,
                    pageSize: 9999
                });
                _groups = _groups?.Items?.map((item: any) => {
                    return {
                        groupKey: item.Key || item.GroupKey,
                        appId: item?.AppId?.replaceAll('{AppId}', appInfo?.AppId),
                    };
                }) || [];
                result = await getMessageGroup({
                    siteOutId: appInfo?.SiteInfo?.OutId,
                    dataVersionId: appInfo?.SiteInfo?.DataVersionId,
                    appId: appInfo?.AppId,
                    group: _groups,
                    isWap: true,
                });
                break;
            case 'customize':
                // 自定义
                const _group = msgGroup?.map((item: any) => {
                    return {
                        groupKey: item.Key || item.GroupKey,
                        appId: item?.AppId?.replaceAll('{AppId}', appInfo?.AppId),
                    };
                }) || [];
                result = await getMessageGroup({
                    siteOutId: appInfo?.SiteInfo?.OutId,
                    dataVersionId: appInfo?.SiteInfo?.DataVersionId,
                    appId: appInfo?.AppId,
                    group: _group,
                    isWap: true,
                });
                break;
            case 'listAndGrid':
                const _gridGroup = iconGrid?.map((item: any) => {
                    return {
                        groupKey: item.Key || item.GroupKey,
                        appId: item?.AppId?.replaceAll('{AppId}', appInfo?.AppId),
                    };
                }) || [];
                result[0] = await getMessageGroup({
                    siteOutId: appInfo?.SiteInfo?.OutId,
                    dataVersionId: appInfo?.SiteInfo?.DataVersionId,
                    appId: appInfo?.AppId,
                    group: _gridGroup,
                    isWap: true,
                });
                const _listGroup = listGroup?.map((item: any) => {
                    return {
                        groupKey: item.Key || item.GroupKey,
                        appId: item?.AppId?.replaceAll('{AppId}', appInfo?.AppId),
                    };
                }) || [];
                result[1] = await getMessageGroup({
                    siteOutId: appInfo?.SiteInfo?.OutId,
                    dataVersionId: appInfo?.SiteInfo?.DataVersionId,
                    appId: appInfo?.AppId,
                    group: _listGroup,
                    isWap: true,
                });
                break;
            default:
                break;
        }
        setData(result);
        setLoading(false);
    }

    useEffect(() => {
        getGroups();
    }, [h5Viewtype, msgGroupLayout, range, msgGroup, iconGrid, listGroup]);

    return [loading, data, setData, getGroups];
}